package com.satoken.controller;

import cn.dev33.satoken.stp.SaLoginModel;
import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserController {


    /**
     * @param name
     * @param password
     * @return java.lang.String
     * @Description
     * @author HuangGaoCan
     * @date 19:29 2023/10/19
     **/
    @RequestMapping("/login")
    public String Login(String name, String password) {
        StpUtil.checkDisable("1001");

        if ("zhangsan".equals(name) && "123456".equals(password)) {
            StpUtil.login("1001", new SaLoginModel()
                    .setDevice("pc")
                    .setIsLastingCookie(true)
                    .setTimeout(60 * 60 * 24 * 7));
            StpUtil.getSession().set("username", name);
            StpUtil.getSession().set("Account-Session", StpUtil.getLoginId());
            StpUtil.getTokenSessionByToken(StpUtil.getTokenValue()).set("Token-Session", StpUtil.getTokenValue());
            return "登录成功";
        }
        return "登录失败";
    }

    /**
     * @return cn.dev33.satoken.util.SaResult token信息
     * @Description 登录方法
     * @author HuangGaoCan
     * @date 20:03 2023/10/19
     **/
    @RequestMapping("/doLogin")
    public SaResult doLogin() {
        StpUtil.login("10001");
        SaTokenInfo tokenInfo = StpUtil.getTokenInfo();
        return SaResult.data(tokenInfo);
    }

    @RequestMapping("/isLogin")
    public String isLogin() {
        String AccountSession = (String) StpUtil.getSession().get("Account-Session");
        String TokenSession = (String) StpUtil.getTokenSession().get("Token-Session");
        String isGet = String.valueOf(StpUtil.isDisable(StpUtil.getLoginId(), "user.get"));
        return AccountSession + "," + TokenSession + "," + "权限是否封禁:" + isGet  +"会话是否登录:" + StpUtil.isLogin();
    }

    @RequestMapping("/logout")
    public String logout() {
        StpUtil.logout();
        return "注销登录";
    }

    @RequestMapping("/checkLogout")
    public String checkLogout() {
        StpUtil.checkLogin();
        return "已登录";
    }

    @RequestMapping("/getLoginId")
    public Object getLoginId() {
        return "当前会话id :" + StpUtil.getLoginId();
    }

    @RequestMapping("/getTokenValue")
    public String getTokenValue() {
        return "当前会话的 token :" + StpUtil.getTokenValue();
    }

    @RequestMapping("/getTokenName")
    public String getTokenName() {
        return "获取当前`StpLogic`的 token 名称:" + StpUtil.getTokenName();
    }


    @RequestMapping("/getTokenTimeout")
    public String getTokenTimeout() {
        return "获取当前会话剩余有效期（单位：s，返回-1代表永久有效）" + StpUtil.getTokenTimeout();
    }

    @RequestMapping("/getTokenInfo")
    public String getTokenInfo() {
        return "获取当前会话的 token 信息参数" + SaResult.data(StpUtil.getTokenInfo());
    }

    @RequestMapping("/logoutById")
    public String logoutById() {
        StpUtil.logout(1001);
        return "1001 注销登录";
    }

    @RequestMapping("/logoutByTokenValue")
    public String logoutByTokenValue(String token) {
        StpUtil.logoutByTokenValue(token);
        return token + "注销登录";
    }

    @RequestMapping("/kickout")
    public String kickout() {
        StpUtil.kickout(1001);
        return "将指定账号踢下线";
    }

}
